<template>
  <el-card class="login-card">
    <h1>用户登录</h1>
    <el-form ref="loginForm" :model="loginForm" :rules="rules" hide-required-asterisk>
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" placeholder="用户名" prefix-icon="el-icon-s-custom"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          placeholder="密码"
          prefix-icon="el-icon-lock"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item>
        <div class="password-tool">
          <el-checkbox v-model="checked">记住密码</el-checkbox>
          <el-link type="primary" title="未开发的功能" :underline="false">忘记密码？</el-link>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('loginForm')" style="width: 100%;">登录</el-button>
      </el-form-item>
    </el-form>
    <p class="register-tip">
      没有账户？
      <router-link to="register">点击注册</router-link>
    </p>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      checked: true,
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios
            .post("/login", JSON.stringify(this.loginForm))
            .then((response) => {
              // console.log(response)
              if (
                response.status === 200
                // Object.prototype.hasOwnProperty.call(response.data, "token")
                // response.data.hasOwnProperty("token")
              ) {
                console.log(response.data);
                let userData = {
                  username: "",
                  email: "",
                  phoneNumber: "",
                }
                userData.username = response.data.username;
                userData.email = response.data.email;
                userData.phoneNumber = response.data.phoneNumber;
                this.$store.commit("login", userData);
                
                // 记住密码
                if (this.checked) {
                  let userinfo = {
                    username: this.loginForm.username,
                    password: this.loginForm.password,
                  };
                  localStorage.setItem('userinfo', JSON.stringify(userinfo));
                }

                this.$router.push("/home");
                this.$message({
                  message: "登陆成功",
                  type: "success",
                });
              } else {
                this.$message.error("用户名或密码错误");
              }
            })
            .catch((error) => {
              console.error(error);
            });
        } else {
          // console.log('error submit!')
          return false;
        }
      });
    },
  },
  created() {
    // console.log(this.$store.state.userData);
    if (this.$store.state.userData !== null) {
      this.$router.push("/home");
    }

    if (localStorage.getItem('userinfo')) {
      const userinfo = JSON.parse(localStorage.getItem('userinfo'));
      this.loginForm.username = userinfo.username;
      this.loginForm.password = userinfo.password;
    }
  },
};
</script>

<style scoped>
/* .login-page {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('../assets/images/sky.jpeg');
} */

h1 {
  font-size: 25px;
}

.el-form {
  padding: 0 10px 0 10px;
}

.el-input {
  margin-top: 15px;
  font-size: 16px;
}

.login-card {
  width: 400px;
}

.password-tool {
  display: flex;
  justify-content: space-between;
  margin: 0 0 15px 0px;
  padding: 0 5px 0 5px;
}

.register-tip {
  color: gray;
  font-size: 12px;
}
</style>
